﻿

using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Travel.CommonUtil.Snowflake;
using Travel.Data;
using Travel.Data.Dtos;
using Travel.Data.Inputs;

namespace Travel.Service.User.Impl
{
    public class MenuButtonService : ServiceBase, IMenuButtonService
    {

        /// <summary>
        /// 添加或者修改 菜单按钮
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Results<int>> UpdateOrAdd(MenuButtonInput input)
        {
            var count = TravelContext.TabMenuButtons.Count(c => !c.Deleted && c.ButtonName == input.ButtonName && c.MenuModuleId == input.MenuModuleId);
            if (count > 0)
            {
                return Results<int>.FailResult("按钮已经存在");
            }
            if (input.Id > 0)
            {
                var entity = await TravelContext.TabMenuButtons.FindAsync(input.Id);
                ObjectMapper.Map(input, entity);
            }
            else
            {
                input.Id = SnowIdWorker.NextId();
                await TravelContext.TabMenuButtons.AddAsync(ObjectMapper.Map<Data.Entity.TabMenuButton>(input));
            }
            return Results<int>.DataResult(await TravelContext.SaveChangesAsync());
        }
        /// <summary>
        /// 菜单按钮查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Results<PageDto<MenuButtonDto>>> MenuButtonQuery(MenuButtonQueryInput input)
        {
            var tabMenus = TravelContext.TabMenuButtons.AsNoTracking().Include(c => c.MenuModule).Where(c => !c.Deleted);

            if (input.MenuModuleId.HasValue && input.MenuModuleId.Value > 0)
            {
                tabMenus = tabMenus.Where(c => c.MenuModuleId == input.MenuModuleId);
            }
            PageDto<MenuButtonDto> page = new PageDto<MenuButtonDto>()
            {

                Total = tabMenus.Count()
            };
            var list = tabMenus.OrderByDescending(c => c.CreatedTime).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToList();
            page.Data = ObjectMapper.Map<List<MenuButtonDto>>(list);
            return Results<PageDto<MenuButtonDto>>.DataResult(page);

        }
        /// <summary>
        /// 根据菜单按钮ID获取一条记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Results<MenuButtonDto>> GetMenuButtonById(long id)
        {
            var tabMenuButton = await TravelContext.TabMenuButtons.AsNoTracking().Include(c => c.MenuModule).FirstOrDefaultAsync(c => c.Id == id);
            if (tabMenuButton == null)
            {
                return Results<MenuButtonDto>.FailResult("查无此按钮");
            }
            return Results<MenuButtonDto>.DataResult(ObjectMapper.Map<MenuButtonDto>(tabMenuButton));
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="loginUserId"></param>
        /// <param name="idList"></param>
        /// <returns></returns>
        public async Task<Results<int>> RemoveMenuButton(params long[] idList)
        {
            if (!idList.Any())
            {
                return Results<int>.FailResult("必选选择一条数据");
            }
            var entityList = TravelContext.TabMenuButtons.Include(c => c.MenuModule).Where(c => !c.Deleted && idList.Contains(c.Id));
            if (!entityList.Any())
            {

                return Results<int>.FailResult("查无此记录");
            }
            await entityList.ForEachAsync(c =>
            {
                c.Deleted = true;
                c.UpdatedUserId = LoginUserId;
            });
            return Results<int>.DataResult(await TravelContext.SaveChangesAsync());
        }
    }
}
